var CourseMgtService = (function () {
	var serviceBase = "http://localhost:8080/EDU/e/coursemgt";
	var categoryServiceBase = "http://173.39.24.47:8080/edu/services/rest/categorymgt";
	var cosRelPath = "/courseofferings";
	var lessonsRelPathCo = "/lessons";
	var unitsRelPathLesson = "/units";
	var jsonContentType = "application/json; charset=utf-8";

	var courseOffering = {};
	courseOffering.code = "Test";
	courseOffering.title = "Course Offering 1";
	courseOffering.lessons = new Array();



	getCategories = function (callback) {
		$.getJSON(categoryServiceBase + "/" + categories, function (data) {
			callback(data);
		});
	};


	getCourseOfferingsTaughtByMe = function (userId, callback) {
		$.getJSON(serviceBase + "/users/" + userId + cosRelPath, function (data) {
			callback(data);
		});

	};

	getCourseOfferingsTaughtToMe = function (userId, callback) {
		$.getJSON(serviceBase + "/users/" + userId + cosRelPath, function (data) {
			callback(data);
		});

	};

	getCourseOfferingsAuthoredByMe = function (userId, callback) {
		$.getJSON(serviceBase + "/users/" + userId + cosRelPath, function (data) {
			callback(data);
		});
	};

	getPublicallyAvailableCourseOfferings = function (callback) {
		// $.getJSON(serviceBase + cosRelPath, function(data) {
		// var json = jQuery.parseJSON('{"courseofferings":' + JSON.stringify(data) +'}');
		// callback(json);
		// });
		$.getJSON("../course/dummy/courseofferings.json", function (data) {
			//alert(JSON.stringify(data));
			callback(data);
		});
	};

	searchCourseOfferings = function (searchString, callback) {
		$.getJSON("../course/dummy/courseofferings.json", function (data) {
			callback(data);
		});
	}
	addCourseOffering = function (courseOffering, callback) {
		alert(JSON.stringify(courseOffering));
		$.ajax({
			type: "POST",
			url: serviceBase + cosRelPath,
			data: JSON.stringify(courseOffering),
			contentType: jsonContentType,
			success: function (json) {
				alert(json);
				callback(json);
			}
		});
	};

	getCourseOfferingDetails = function (courseOfferingCode, callback) {
		alert(courseOfferingCode);
		$.getJSON("../course/dummy/courseofferingdetails.json", function (data) {
			callback(data);
		});
		// $.getJSON(serviceBase + cosRelPath + "/" + courseOfferingCode, function(data) {
		// callback(data);
		// });
	}
	addLesson = function (lesson, courseOfferingCode, callback) {
		//		$.ajax({
		//			type : "POST",
		//			url : serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo,
		//			data : lesson,
		//			contentType : "json",
		//			dataType : "json",
		//			processdata : true,
		//			success : function(json) {
		//				callback(json);
		//			}
		//		});
		courseOffering.lessons.push(lesson);
		callback(courseOffering);
	};

	updateLesson = function (lesson, courseOfferingCode, callback) {
		$.ajax({
			type: "PUT",
			url: serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo,
			data: lesson,
			contentType: "json",
			dataType: "json",
			processdata: true,
			success: function (json) {
				callback(json);
			}
		});
	};

	deleteLesson = function (courseOfferingCode, lessonName, callback) {
		$.ajax({
			type: "DELETE",
			url: serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo + "/" + lessonName,
			data: "",
			contentType: "",
			dataType: "json",
			processdata: true,
			success: function (json) {
				callback(json);
			}
		});
	};

	getLesson = function (courseOfferingCode, lessonName, callback, returnAsIs) {
		// $.getJSON(serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo + "/" + lessonName, function(data) {
		// callback(data);
		// });
		$.getJSON("../course/dummy/courseofferinglessondetails.json", function (data) {
			callback(data, returnAsIs);
		});

	};

	getLessons = function (courseOfferingCode, callback) {
		$.getJSON(serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo, function (data) {
			callback(data);
		});
	};

	addLessonUnit = function (lessonUnit, lessonName, courseOfferingCode, callback) {
		//		$.ajax({
		//			type : "POST",
		//			url : serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo + "/" + lessonName + "/units",
		//			data : lessonUnit,
		//			contentType : "json",
		//			dataType : "json",
		//			processdata : true,
		//			success : function(json) {
		//				callback(json);
		//			}
		//		});
		for (var i = 0; i < courseOffering.lessons.length; i++) {
			var lesson = courseOffering.lessons[i];
			if (lesson.name == lessonName) {
				if (lesson.units == undefined) {
					lesson.units = new Array();
				}
				lesson.units.push(lessonUnit);
				break;
			}
		}
	};

	updateLessonUnit = function (lessonUnit, lessonName, courseOfferingCode, callback) {
		$.ajax({
			type: "PUT",
			url: serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo + "/" + lessonName + "/units",
			data: lessonUnit,
			contentType: "json",
			dataType: "json",
			processdata: true,
			success: function (json) {
				callback(json);
			}
		});
	};

	getLessonUnits = function (courseOfferingCode, lessonName, callback) {
		$.getJSON(serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo + "/" + lessonName + "/units", function (data) {
			callback(data);
		});
	};

	getLessonUnit = function (courseOfferingCode, lessonName, lessonUnitName) {
		$.getJSON(serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo + "/" + lessonName + "/units/" + lessonUnitName, function (data) {
			callback(data);
		});
	};
	deleteLessonUnit = function (courseOfferingCode, lessonName, lessonUnitName) {
		$.ajax({
			type: "DELETE",
			url: serviceBase + cosRelPath + "/" + courseOfferingCode + lessonsRelPathCo + "/" + lessonName + "/units/" + lessonUnitName,
			data: "",
			contentType: "",
			dataType: "json",
			processdata: true,
			success: function (json) {
				callback(json);
			}
		});

	};
	return {
		getCourseOfferingsTaughtByMe: getCourseOfferingsTaughtByMe,
		getCourseOfferingsTaughtToMe: getCourseOfferingsTaughtToMe,
		getCourseOfferingsAuthoredByMe: getCourseOfferingsAuthoredByMe,
		searchCourseOfferings: searchCourseOfferings,
		getCourseOfferingDetails: getCourseOfferingDetails,
		addCourseOffering: addCourseOffering,
		addLesson: addLesson,
		updateLesson: updateLesson,
		deleteLesson: deleteLesson,
		getLesson: getLesson,
		getLessons: getLessons,
		addLessonUnit: addLessonUnit,
		updateLessonUnit: updateLessonUnit,
		getLessonUnits: getLessonUnits,
		getLessonUnit: getLessonUnit,
		deleteLessonUnit: deleteLessonUnit,
		getPublicallyAvailableCourseOfferings: getPublicallyAvailableCourseOfferings,
	};
})();